package ac.rsync.pojo;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import ac.java.utils.AcFile;
import ac.java.utils.AcFileMd5;
import ac.java.utils.Md5Util;

public class AcDirFiles {

	public static Document getFsXml(String path) {
		Map<String,Map> fsMap = AcFile.listMd5Files(path);
		
		Document xml = DocumentHelper.createDocument();
		
	    Element dataRoot = xml.addElement("data");
	    Element filesEle = dataRoot.addElement("files");
	    Element dirsEle = dataRoot.addElement("dirs");
	    
	    if(fsMap.containsKey("f"))
	    {
	    	Map<String,String> fMap = fsMap.get("f");
	    	try {
				Iterator fIt = fMap.entrySet().iterator();
				while (fIt.hasNext()) {
					Entry entry = (Entry) fIt.next();
					String key = (String) entry.getKey();
					Element fileE = filesEle.addElement("file");
					
					fileE.addElement("path").addText(key);
					fileE.addElement("key").addText(fMap.get(key));
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
	    }
		
	    if(fsMap.containsKey("d"))
	    {
	    	Map<String,String> dMap = fsMap.get("d");
	    	try {
				Iterator dIt = dMap.entrySet().iterator();
				while (dIt.hasNext()) {
					Entry entry = (Entry) dIt.next();
					String key = (String) entry.getKey();
					Element dirE = dirsEle.addElement("file");
					
					dirE.addElement("path").addText(key);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
	    }
		
		return xml;
	}
	
	public static String getFsXmlStr(String path)
	{
		return getFsXml(path).asXML();
	}

	public static void main(String[] args) throws Exception {
		String path = "D:\\sql";
		String xml = AcDirFiles.getFsXmlStr(path);
		System.out.println(xml);
	}
}
